package alps.common.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import alps.common.R;

/**
 * 该组件由图片，文本组成：
 *      -------------
 *      |           |
 *      |    img    |
 *      |           |
 *      ------------
 *          xyz
 * 目前支持以下属性：
 * <attr name="img" format="reference" />               图片
 * <attr name="textColor" format="color" />             字体颜色
 * <attr name="textSize" format="dimension" />          字体大小
 * <attr name="text" format="reference|string" />       文字内容
 * <attr name="imgAndTxtSpace" format="dimension"/>     图片与文字垂直距离
 * <attr name="imgMargin" format="dimension" />         图片与控件左右边缘的边距
 *
 * Created by alps on 2017/2/21.
 */

public class IconWithText extends LinearLayout {

    private ImageView mImageView;

    private TextView mTextView;

    public IconWithText(Context context) {
        this(context, null);
    }

    public IconWithText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IconWithText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        final TypedArray attributesArray = context.obtainStyledAttributes(attrs, R.styleable.iconWithText);
        final int layoutResId = R.layout.icon_with_text;

        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(layoutResId, this, true);
        mImageView = (ImageView) findViewById(R.id.img);
        mTextView = (TextView) findViewById(R.id.content);

        Drawable d = attributesArray.getDrawable(R.styleable.iconWithText_alps_img);
        if (d != null) {
            mImageView.setImageDrawable(d);
        }

        ColorStateList textColor = attributesArray.getColorStateList(R.styleable.iconWithText_alps_textColor);
        if (textColor != null) {
            mTextView.setTextColor(textColor);
        }

        int textSize = attributesArray.getDimensionPixelSize(R.styleable.iconWithText_alps_textSize, 0);
        if (textSize != 0) {
            Log.d("Alps", "textSize:" + textSize);
            mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        }

        String text = attributesArray.getString(R.styleable.iconWithText_alps_text);
        if (text != null) {
            mTextView.setText(text);
        }

        int imgMargin = attributesArray.getDimensionPixelSize(R.styleable.iconWithText_alps_imgMargin, 0);
        if (imgMargin > 0) {
            LinearLayout.LayoutParams layoutParams = (LayoutParams) mImageView.getLayoutParams();
            layoutParams.leftMargin = imgMargin;
            layoutParams.rightMargin = imgMargin;
        }

        int topMargin = attributesArray.getDimensionPixelSize(R.styleable.iconWithText_alps_imgAndTxtSpace, 0);
        if (topMargin > 0) {
            LinearLayout.LayoutParams layoutParams = (LayoutParams) mTextView.getLayoutParams();
            layoutParams.topMargin = topMargin;
        }

        attributesArray.recycle();
    }

    public void setText(int resId) {
        mTextView.setText(resId);
    }

    public void setText(String str) {
        mTextView.setText(str);
    }

    public void setTextSize(float dpSize) {
        mTextView.setTextSize(dpSize);
    }

    public void setTextSize(int unit, float size) {
        mTextView.setTextSize(unit, size);
    }

    public void setImage(int resId) {
        mImageView.setImageResource(resId);
    }

    public void setImageDrawable(Drawable drawable) {
        mImageView.setImageDrawable(drawable);
    }

    public void setTextColor(ColorStateList textColor) {
        mTextView.setTextColor(textColor);
    }

    public void setTextColor(int color) {
        mTextView.setTextColor(color);
    }

    public TextView getTextView() {
        return mTextView;
    }

    public ImageView getImageView() {
        return mImageView;
    }
}
